/**
 * AppLog.java
 *
 * @author liujie11
 * 
 * @date 2014-3-11
 * 
 * Copyright 2014 Baidu. All rights reserved. 
 */
package com.baidu.common.utils;

import java.io.FileOutputStream;
import java.io.PrintStream;

import android.os.Environment;
import android.util.Log;

public class AppLog {
    public final static int LEVEL_ERROR = 0;
    public final static int LEVEL_WARN = 1;
    public final static int LEVEL_INFO = 2;
    public final static int LEVEL_DEBUG = 3;
    public final static int LEVEL_VERBOSE = 4;
    
    public static int log_level = LEVEL_DEBUG;
    
    public static void error(String tag, String msg) {
        
        if (log_level >= LEVEL_ERROR) {
            Log.e(tag, msg);
        }
    }
    
    public static synchronized void f(String tag,String msg){
        if (log_level >= LEVEL_ERROR) {
            Log.e(tag, msg);
            write(TimeUtils.getFullDate(System.currentTimeMillis()) + " - " +msg + "\n");
        }
    } 
    
    public static void write(String log){

        String path = Environment.getExternalStorageDirectory().getPath();
        
        path+="//log_lxb"  +".txt";

        FileOutputStream baos = null;  
        PrintStream printStream = null;  
        try 
        {  
            baos = new FileOutputStream(path,true); 
            baos.write(log.getBytes());
        } 
        catch (Exception e) 
        {  
            e.printStackTrace();  
        } 
        finally 
        {  
            try 
            {  
                if (printStream != null) 
                {  
                   printStream.close();  
                }  
                if (baos != null) 
                {  
                    baos.close();  
                }  
            } 
            catch (Exception e){}  
        } 
    
    }
    
    public static void error(String tag, String msg, Throwable t) {
        
        if (log_level >= LEVEL_ERROR) {
            Log.e(tag, msg, t);
        }
    }
    
    public static void verbose(String tag, String msg) {
        
        if (log_level >= LEVEL_VERBOSE) {
            Log.v(tag, msg);
        }
    }
    
    public static void verbose(String tag, String msg, Throwable t) {
        
        if (log_level >= LEVEL_VERBOSE) {
            Log.v(tag, msg, t);
        }
    }
    
    public static void warn(String tag, String msg) {
        
        if (log_level >= LEVEL_WARN) {
            Log.w(tag, msg);
        }
    }
    
    public static void warn(String tag, String msg, Throwable t) {
        
        if (log_level >= LEVEL_WARN) {
            Log.w(tag, msg, t);
        }
    }
    
    public static void info(String tag, String msg) {
        
        if (log_level >= LEVEL_INFO) {
            Log.i(tag, msg);
        }
    }
    
    public static void info(String tag, String msg, Throwable t) {
        
        if (log_level >= LEVEL_INFO) {
            Log.i(tag, msg, t);
        }
    }
    
    public static void debug(Object obj, String msg) {
        debug(obj.getClass().getCanonicalName(), msg);
    }
    
    public static void debug(String tag, String msg) {
        if (log_level >= LEVEL_DEBUG) {
            Log.d(tag, msg);
        }
    }
    
    public static void debug(String tag, String msg, Throwable t) {
        
        if (log_level >= LEVEL_DEBUG) {
            Log.d(tag, msg, t);
        }
    }
}
